<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Controller {

	public function index(){
            $email=filter_input(INPUT_POST,'email',FILTER_SANITIZE_EMAIL);
            $password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
            $this->load->model('user');// loading user model
            $this->load->model("skills");// loading the skill model
            $this->load->model("Message");
            $result=$this->user->validateUser($email,$password,TRUE);
            $isLogin = $this->session->userdata('is_login');
            if($isLogin==TRUE){
                $hasSession=$isLogin;
            }else{
                $hasSession=FALSE;
            }
            if($result['success']===TRUE){// this is the normal email and password case
                $curUser = $result['user'];// get the user object
                $uid = $curUser->id;
                $session=array('email'=>$email,'is_login'=>TRUE,'user_id'=>$uid);
                //setting the data into the session
                $this->session->set_userdata($session);
                $data['user']=$curUser;
                $data['skills']=$this->skills->getSkillsById($uid);
                $data['educations']=$this->user->getEducationByUid($uid);
                $data['certificates']=$this->user->getCertificatesByUid($uid);
                $data['messages']=$this->Message->getAllMessagesByUID($uid);
              
                $this->load->view('profilepage',$data);
            }else{
                // check the session
                if($hasSession){
                    $uid = $this->session->userdata("user_id");
                    $curUser=$this->user->getUser($uid);
                    $data['user']=$curUser;
                    $data['skills']=$this->skills->getSkillsById($uid);
                    $data['educations']=$this->user->getEducationByUid($uid);
                    $data['certificates']=$this->user->getCertificatesByUid($uid);
                    $data['messages']=$this->Message->getAllMessagesByUID($uid);
                    $this->load->view('profilepage',$data);
                }else{
                    // this mean we have to redirect user back to the home page
                    header("Location: ".base_url()."/index.php");
                }
            }
	}
        
        public function signup(){
            $email=filter_input(INPUT_POST,'email',FILTER_SANITIZE_EMAIL);
            $password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
            $firstname=filter_input(INPUT_POST,'firstname',FILTER_SANITIZE_STRING);
            $lastname=filter_input(INPUT_POST,'lastname',FILTER_SANITIZE_STRING);
            $this->load->model('user');// loading user model
            $rs=$this->user->addUser($email,$password,$lastname,$firstname);          
           
            if($rs['success']===TRUE){
                // this mean user is good to go
                $session=array('email'=>$email,'is_login'=>TRUE,'user_id'=>$rs['id']);
                //setting the data into the session
                $this->session->set_userdata($session);
                header("Location: ".base_url()."index.php/profile");
            }else{
                 header("Location: ".base_url()."index.php");
            }
        }
        
        public function checkemailexist(){
             $email=filter_input(INPUT_POST,'email',FILTER_SANITIZE_EMAIL);
             $this->load->model('user');// loading user model
             $response = NULL;
             $userExist = $this->user->checkEmailExist($email);
             if($userExist){
                 $response = array("success"=>FALSE,"message"=>"This email exist");
             }else{
                 $response=array("success"=>TRUE);
             }
             echo json_encode($response);
        }
        public function uservalidation(){
            $email=filter_input(INPUT_POST,'email',FILTER_SANITIZE_EMAIL);
            $password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
            $this->load->model('user');// loading user model
            echo json_encode($this->user->validateUser($email,$password));
        }
        
        public function signout(){
            $session=array('email'=>'','is_login'=>'','user_id'=>'');
            $this->session->unset_userdata($session);// remove the session
            header("Location: ".base_url()."index.php");
        }
        public function editdata(){
            $value=filter_input(INPUT_POST,'val',FILTER_SANITIZE_STRING);
            $table=filter_input(INPUT_POST,'table',FILTER_SANITIZE_STRING);
            $name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING);
            $uid = $uid = $this->session->userdata("user_id");
            if($uid!=NULL){
            
                $sql = "UPDATE ".$table." SET ".$name." = '".$value. "' WHERE id=".$uid;
                $this->db->query($sql);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow >0){
                    // meaning some row has been affected
                    echo json_encode(array("success"=>TRUE));
                }else{
                    echo json_encode(array("success"=>FALSE));
                }
            }
        }
        public function editselectdata(){
            $value=filter_input(INPUT_POST,'val',FILTER_SANITIZE_STRING);
            $table=filter_input(INPUT_POST,'table',FILTER_SANITIZE_STRING);
            $name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING);
            $pk = filter_input(INPUT_POST,'pk',FILTER_SANITIZE_STRING);
            $uid = $uid = $this->session->userdata("user_id");
            
            if($uid!=NULL){
                $sql = "UPDATE ".$table." SET ".$name." = '".$value."' WHERE USER_ID= ".$uid." AND id=".$pk;
                $this->db->query($sql);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow >0){
                    // meaning some row has been affected
                    echo json_encode(array("success"=>TRUE));
                }else{
                    echo json_encode(array("success"=>FALSE));
                }
            }
        }
        
        public function addeducation(){
            $uid = $uid = $this->session->userdata("user_id");
            if($uid!=NULL){
                $school=filter_input(INPUT_POST,'school',FILTER_SANITIZE_STRING);
                $degree=filter_input(INPUT_POST,'degree',FILTER_SANITIZE_STRING);
                $major=filter_input(INPUT_POST,'major',FILTER_SANITIZE_STRING);
                $sql = "INSERT into user_education (SCHOOL_ID,DEGREE_ID,USER_ID,MAJOR_ID) VALUES ($school,$degree,$uid,$major)";
                $this->db->query($sql);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow>0){
                    echo json_encode(array('success'=>TRUE));
                }else{
                    echo json_encode(array('success'=>FALSE));
                }

            }
            
        }
        
        public function addskill(){
             $uid = $uid = $this->session->userdata("user_id");
             if($uid!=NULL){
                 $skill=filter_input(INPUT_POST,'skill',FILTER_SANITIZE_STRING);
                $prof=filter_input(INPUT_POST,'proficiency',FILTER_SANITIZE_NUMBER_INT);
                $comment=filter_input(INPUT_POST,'comment',FILTER_SANITIZE_STRING);
                
                $sql = "INSERT into skills (USER_ID,SKILL_ID,COMMENTS,SKILL_PROF) VALUES ($uid,$skill,'$comment',$prof)";
                $this->db->query($sql);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow>0){
                    echo json_encode(array('success'=>TRUE));
                }else{
                    echo json_encode(array('success'=>FALSE));
                }
             }
        }
        
        public function addcertificate(){
            $uid = $uid = $this->session->userdata("user_id");
            if($uid!=NULL){
                $certificate=filter_input(INPUT_POST,'certificate',FILTER_SANITIZE_STRING);
                $dateAcquired=filter_input(INPUT_POST,'acquiredDate',FILTER_SANITIZE_STRING);
                $dateAcquired=  str_replace('/', '-', $dateAcquired);
               $newFormat= DateTime::createFromFormat('m-d-Y', $dateAcquired)->format('Y-m-d');

                 $sql = "INSERT into user_certificates (CERTIFICATE_ID,ACQUIRED_DATE,USER_ID) VALUES ($certificate,'$newFormat',$uid)";
                $this->db->query($sql);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow>0){
                    echo json_encode(array('success'=>TRUE));
                }else{
                    echo json_encode(array('success'=>FALSE));
                }
            }
        }
        
        public function removeData(){
            $table=filter_input(INPUT_POST,'table',FILTER_SANITIZE_STRING);
            $pk=  filter_input(INPUT_POST,'pk',FILTER_SANITIZE_STRING);
            $sql="DELETE FROM $table WHERE id=$pk";
            $resp=$this->db->simple_query($sql);
            echo json_encode(array('success'=>$resp));
        }
        

}
